package org.example.game.history;

import java.util.HashMap;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/06/30 11:39
 */
public class Solution10358 {

    public static void main(String[] args) {
        System.out.println(new Solution10358().maximumLength(new int[]{2,4,7,1,2,6,7}, 8));
    }

    public int maximumLength(int[] nums, int k) {

        HashMap<String, Integer> countMap = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            nums[i] = nums[i] % k;
            if(i>0){
                String key = "" + nums[i-1] + nums[i];
                countMap.putIfAbsent(key, 0);
                countMap.put(key, countMap.get(key) + 1);
            }
        }

        int max = 1;
        for (Integer value : countMap.values()) {
            max = Math.max(max, value);
        }

        int maxIndex = 0;

        if(max > nums.length /2){
            for (String s : countMap.keySet()) {
                //  11, 12 , 21 ,22
                int a = Integer.parseInt(s.substring(0,1));
                int b = Integer.parseInt(s.substring(1,2));

                int index = 0;
                for (int j = 0; j < nums.length; j++) {
                    if(index%2 == 0 && a == nums[j]){
                        index++;
                    }else if(index%2 == 1 && b == nums[j]){
                        index++;
                    }
                }
                maxIndex = Math.max(index, maxIndex);
            }
        }else {
            for (int i = 0; i < k; i++) {
                for (int x = 0; x < k; x++) {
                    int a = i;
                    int b = x;

                    int index = 0;
                    for (int j = 0; j < nums.length; j++) {
                        if(index%2 == 0 && a == nums[j]){
                            index++;
                        }else if(index%2 == 1 && b == nums[j]){
                            index++;
                        }
                    }
                    maxIndex = Math.max(index, maxIndex);
                }
            }
        }

        return maxIndex;
    }

}
